package com.teame.readbaby;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teame.readbaby.dao.AddressDao;
import com.teame.readbaby.parser.AddressListParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.CopyFileUtils;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.AddressListInfo;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 地址列表
 * 
 * @author dupengtao
 */
public class AddressListActivity extends BaseActivity {

	protected static final String TAG = "AddressListActivity";

	private SimpleAdapter simpleAdapter;
	private ListView address_list_list;
	private TextView address_list_null_text;
	
	private TextView head_paycenter_text;
	private TextView address_list_add_text;

	/**
	 * 你懂的
	 */
	@Override
	protected void findViewById() {
		
		address_list_list = (ListView) this.findViewById(R.id.address_list_list);  
		address_list_null_text = (TextView) this.findViewById(R.id.address_list_null_text);
		
		head_paycenter_text =(TextView) this.findViewById(R.id.head_paycenter_text);
		address_list_add_text=(TextView) this.findViewById(R.id.address_list_add_text);
	}

	/**
	 * 加载布局文件
	 */
	@Override
	protected void loadViewLayout() {
		this.setContentView(R.layout.address_list_activity);
	}

	/**
	 * 后台请求数据 大家都按照我这个方法的方式去写
	 */
	@Override
	protected void processLogic() {
		// 1,请求的url地址
		String url = ConstantValue.HOST + ConstantValue.DETAILEADDRESS;
		// 2,请求的参数，没有为null
		// 如果有参数 这样写
		// Map<String, String> params=new HashMap<String, String>();
		// params.put("version", "1");
		// 3.json数据解析器，大家需要按照自己获取json的结构自己编写，要继承baseParser
		// 4,回调函数

		this.getDataFromServer(url, null, new AddressListParser(),
				new DataCallback<List<AddressListInfo>>() {

					@Override
					public void processData(List<AddressListInfo> paramObject,
							boolean paramBoolean) {

						final File addressDBFile = new File(
								AddressListActivity.this.getFilesDir(),
								"citydatabase.db");
						File f = CopyFileUtils.copyAssetsFile(
								AddressListActivity.this, "citydatabase.db",
								addressDBFile.getAbsolutePath());
						if (addressDBFile.exists()
								&& addressDBFile.length() > 0) {
							Logger.i(TAG, "文件已经存在,不需要再次拷贝");
						} else {
							if (f == null) {
								Logger.i(TAG, "拷贝失败");
							} else {
								Logger.i(TAG, "拷贝成功");
							}
						}

						if (!paramBoolean || paramObject == null) {// 获取失败
							showNoAddress();// 显示没有地址
						} else {// 获取成功
							
							AddressDao dao = new AddressDao();
							
							List<Map<String, String>> data = new ArrayList<Map<String, String>>();
							for (AddressListInfo info : paramObject) {
								Map<String, String> map = new HashMap<String, String>();
								map.put("姓名", info.getName());
								map.put("电话", info.getPhonenumber());
								map.put("地址1",dao.getCityName(info.cityid, info.areaid)+info.getAreadetail());
								map.put("地址2",info.getAreadetail());
								
								data.add(map);
							}
							String[] from = { "姓名", "电话", "地址1", "地址2" };
							int[] to = { R.id.addressnameTV, R.id.phoneTV,
									R.id.address0TV,R.id.addressdetilTV};
							simpleAdapter = new SimpleAdapter(
									AddressListActivity.this, data,
									R.layout.address_list_items, from, to);
							address_list_list.setAdapter(simpleAdapter);
						}

					}

				});
	}

	private void showNoAddress() {
		address_list_null_text.setVisibility(View.VISIBLE);
	}

	/**
	 * 设置监听器
	 */
	@Override
	protected void setListener() {
		//
		head_paycenter_text.setOnClickListener(this);
		address_list_add_text.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View paramView) {
		// TODO Auto-generated method stub
		switch (paramView.getId()) {
		case R.id.head_paycenter_text:
			Toast.makeText(this, "连接结算中心界面", 0).show();
			break;
		case R.id.address_list_add_text:
			//dpt:采用一个新方法
			Intent intent = new Intent();
			intent.setComponent(new ComponentName(this, AddAddressActivity.class));
			startActivity(intent);
			break;
		}
	}

}
